package com.example.a14110100409.yuejiandemo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.a14110100409.yuejiandemo.R;
import com.example.a14110100409.yuejiandemo.javabean.User;
import com.example.a14110100409.yuejiandemo.javabean.UserInfo;
import com.example.a14110100409.yuejiandemo.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserEditActivity extends AppCompatActivity {
    private Button edit_btn_return,edit_btn_save;
    private EditText edit_username,edit_password,edit_passwordagain;
    private Intent intent;
    private int user_id;
    private String user_name,user_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_edit);
        initViews();
        initEvents();
        initData();
    }
    private void initViews(){
        edit_username= (EditText) findViewById(R.id.username);
        edit_password= (EditText) findViewById(R.id.edit_pass);
        edit_passwordagain= (EditText) findViewById(R.id.edit_passagain);
        edit_btn_save= (Button) findViewById(R.id.btn_save);
        edit_btn_return= (Button) findViewById(R.id.btn_return);
        user_id = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id",0);
        user_name = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("username","0");

    }
    private void initEvents(){
        edit_btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        edit_btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });

    }
    private void initData(){
        edit_username.setText(user_name);
    }
    private void save(){
        UserModel userModel = new UserModel();
        Call<Integer> call = userModel.updatePwd(user_id,edit_password.getText().toString());
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                Integer result = response.body();
                String str_psd =edit_password.getText().toString();
                String str_psdagain =edit_passwordagain.getText().toString();
                if(str_psd.equals(str_psdagain)){
                    if(result==0){
                        Toast.makeText(UserEditActivity.this, "修改密码失败", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(UserEditActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(UserEditActivity.this,   "两次密码输入不相同", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                Toast.makeText(UserEditActivity.this,   "获取失败", Toast.LENGTH_SHORT).show();
            }
        });
        /*UserModel userModel = new UserModel();
        Call<User> call = userModel.updatePwd(user_id,edit_password.getText().toString());
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User result = response.body();
                String str_psd =edit_password.getText().toString();
                String str_psdagain =edit_passwordagain.getText().toString();
                if(str_psd.equals(str_psdagain)){
                    if(result.equals("0")){
                        Toast.makeText(UserEditActivity.this, "修改密码失败", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(UserEditActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(UserEditActivity.this,   "两次密码输入不相同", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });*/
    }
}
